#pragma once

#include "../core/Common.h"

#ifdef USE_CURL

struct curl_slist;

namespace LuaGui {
	class Curl {
	public:
		Curl();
		~Curl();

		uint64_t GetUID() { return (uint64_t)this; }
		int GetStatusCode() { return m_status_code; }
		std::string& GetText() { return m_text; }
		bool IsFinish() { return m_finish; }
		Curl* AppendHeader(std::string header);
		Curl* SetAuth(std::string username, std::string password);
	public:
		Curl* Get(std::string url);
		Curl* Delete(std::string url);
		Curl* Put(std::string url, std::string data);
		Curl* Post(std::string url, std::string data);
		Curl* Download(std::string url, std::string filename);
		Curl* SendMail(std::string url, std::string text, std::string from, std::string to, std::vector<std::string> cc, std::string username, std::string password);

		bool SyncRequest();
		bool AsyncRequest();
	public:
		virtual void OnFinish(std::string data);
	public:
		static void StaticInit();
		static void StaticCleanup();
	protected:
		Curl* Finish();
	private:
		FILE* m_file;
		std::ostringstream* m_oss;
		std::istringstream* m_iss;
		std::ofstream* m_ofs;
		void* m_easy_handle;
		struct curl_slist* m_slist_request_headers;
		struct curl_slist* m_slist_mail_cc;
		int m_status_code;
		std::string m_text;  // response body
		bool m_finish;
	};
}

#endif
